//
//  ChannelSelectView.m
//  ZYYD-Iphone
//
//  Created by yuzhou on 12-9-21.
//  Copyright (c) 2012年 yuzhou. All rights reserved.
//

#import "IndexSelectView.h"
#import <QuartzCore/QuartzCore.h>
#import "AppDelegate.h"

@implementation IndexSelectView

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    popImageView.image = [[UIImage imageNamed:@"pop_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 20, 45)];
    
    UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)] autorelease];
    [backView addGestureRecognizer:tap];
}

- (void)updateDisplay
{
    [self adjustPopView];
    [self drawButtons];
    [self popAnimate];
}

- (void)adjustPopView
{
    CGRect rect = contentView.frame;
    if (self.isShelf) {
        rect.origin.x = 85;
    }
    else {
        rect.origin.x = 10;
    }
    rect.size.height = self.indexArray.count * 35 + 5;
    rect.origin.y = self.bounds.size.height - 47 - rect.size.height;
    contentView.frame = rect;
}

- (void)popAnimate
{
    CGAffineTransform transform = contentView.transform;
    contentView.transform = CGAffineTransformMakeScale(0.8, 0.8);
    contentView.alpha = 0;
    [UIView animateWithDuration:0.1 animations:^{
        contentView.transform = CGAffineTransformMakeScale(1.1, 1.1);
        contentView.alpha = 1;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            contentView.transform = CGAffineTransformMakeScale(0.9, 0.9);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.1 animations:^{
                contentView.transform = transform;
            } completion:^(BOOL finished) {
            }];
        }];
    }];
}

- (void)drawButtons
{
    for (UIButton *button in contentView.subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            [button removeFromSuperview];
        }
    }
    for (NSDictionary *dic in _indexArray) {
        int index = [_indexArray indexOfObject:dic];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = [[dic objectForKey:@"index"] intValue];
        button.frame = CGRectMake(5, index * 30 + 5, 80, 30);
        [button setImage:[UIImage imageNamed:[dic objectForKey:@"image"]] forState:UIControlStateNormal];
        [button setTitle:[dic objectForKey:@"name"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];
        [button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
        [button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [button.titleLabel setShadowOffset:CGSizeMake(1, 1)];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:button];
    }
}

- (void)buttonAction:(UIButton *)sender
{
    [[UIApplication rootViewController] setCurrentIndex:sender.tag];
    [self dismiss];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.3 animations:^{
        contentView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)dealloc {
    [backView release];
    [contentView release];
    [_indexArray release];
    [popImageView release];
    [super dealloc];
}
@end
